package com.example.club_mvvm

import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.example.club_mvvm.base.RequestEvent
import com.example.club_mvvm.base.a
import com.example.club_mvvm.base.observeWithLifecycle
import com.example.club_mvvm.http_hilt.mall_api.model.request.AdInfoRequestModel
import com.example.club_mvvm.viewmodel.MainViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
    //by 委托给viewModels创建，底层还是ViewModelProvider
    //viewModels:ComponentActivity的扩展函数
    private val viewModel:MainViewModel by viewModels()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        initData()
        initView()
        initListener()

    }

    private fun initListener() {
        //订阅LiveData解析广告结果
        viewModel.appUpdateLiveData.observe(this){
            Log.d("TAG", "广告结果: $it")
        }
        //订阅LiveData解析结果
        viewModel.appUpdateLiveData.observe(this){
            Log.d("TAG", "版本更新结果: $it")
        }
        //订阅Livedata
        viewModel.data1.observe(this){
            Log.d("TAG", "获得数据: $it")
        }
        //订阅流
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED){//生命周期：只有页面处于活跃状态才会订阅信息
                viewModel.data2.collectLatest{
                    when(it){
                        is RequestEvent.Success -> {
                            Toast.makeText(this@MainActivity,"成功",Toast.LENGTH_SHORT).show()
                        }
                        is RequestEvent.Failed -> {
                            Toast.makeText(this@MainActivity,"失败",Toast.LENGTH_SHORT).show()
                        }
                        is RequestEvent.Loading -> {
                            Toast.makeText(this@MainActivity,"加载中",Toast.LENGTH_SHORT).show()
                        }
                        else -> {
                            Toast.makeText(this@MainActivity,"默认",Toast.LENGTH_SHORT).show()
                        }
                    }
                }
            }
        }
        //订阅流，封装扩展函数()
        var str = "demo"
        var st2 = str.a()
        viewModel.data3.observeWithLifecycle(this){
            when(it){
                        is RequestEvent.Success -> {
                            Toast.makeText(this@MainActivity,"成功",Toast.LENGTH_SHORT).show()
                        }
                        is RequestEvent.Failed -> {
                            Toast.makeText(this@MainActivity,"失败",Toast.LENGTH_SHORT).show()
                        }
                        is RequestEvent.Loading -> {
                            Toast.makeText(this@MainActivity,"加载中",Toast.LENGTH_SHORT).show()
                        }
                        else -> {
                            Toast.makeText(this@MainActivity,"默认",Toast.LENGTH_SHORT).show()
                        }
                    }
        }


    }

    private fun initView() {

    }

    private fun initData() {
        val params = mutableMapOf<String,String>()
        params["currentPage"] = "1"
        params["pageSize"] = "10"
//        viewModel.getVideoData(params)
//        viewModel.getVideoData2(params)
//        viewModel.getVideoData3(params)

        //发起网络请求判断版本是否需要更新
//        viewModel.appLevelCheck()

        //发起网络请求获得广告数据
        val requestModel = AdInfoRequestModel(adType =  "skl_app_start")
        viewModel.cmallAdInfo(requestModel.toBody())
    }


}
